package cn.miaoyao.miaoyaoms.controller;

import cn.miaoyao.miaoyaoms.entity.Limits;
import cn.miaoyao.miaoyaoms.entity.MenuInfo;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: shushu
 * @Date: 2023/04/17/12:54
 * @Description:根据用户的权限动态地生成json文件。
 */
@RestController
public class InitController {
    @GetMapping("/init")
    public String getInitInfo(HttpServletRequest request){
        Limits limits = (Limits) request.getSession().getAttribute("limits");//获取session域当中的权限数据
        Map<String, Object> map = new HashMap<>();
        Map<String,Object> homeInfo=new HashMap<>();
        HashMap<String, Object> logoInfo = new HashMap<>();
        List<MenuInfo> datas=new ArrayList<>();
        ArrayList<MenuInfo> menuInfos=null;
        homeInfo.put("title","首页");
        if(limits.getUpdatepermission()==1||limits.getAdduser()==1||limits.getUpdatemiaosample()==1||limits.getAddmiaosample()==1
        ||limits.getDeletemiaosample()==1) {
            homeInfo.put("href", "page/welcome1.html?t=1");
        }else{
            homeInfo.put("href","page/welcome.html?t=1");
        }
        logoInfo.put("title","苗药管理系统");
        logoInfo.put("image","images/logo.png");
        logoInfo.put("href","");
        if(limits.getAdduser()==1||limits.getDeleteuser()==1){
            menuInfos = new ArrayList<>();
            menuInfos.add(MenuInfo.secondAdd("用户列表","page/user_list.html","fa fa-users","_self"));
            menuInfos.add(MenuInfo.secondAdd("用户添加","page/user_add.html","fa fa-user-plus","_self"));
            datas.add(MenuInfo.firstAdd("用户管理","","fa fa-user","_self",menuInfos));//一级导航下的是二级导航。
        }
        //判断是否有权限操控别人的权限
        if(limits.getUpdatepermission()==1){
            menuInfos=new ArrayList<>();
            menuInfos.add(MenuInfo.secondAdd("权限列表","page/permission_list.html","fa fa-users","_self"));
            menuInfos.add(MenuInfo.secondAdd("公告发布","page/notice.html","fa fa-bullhorn","_self"));
            datas.add(MenuInfo.firstAdd("权限管理","","fa fa-key","_self",menuInfos));
        }
        //看是否有药材相关的权限
        if(limits.getAddmiaosample()==1){
            menuInfos=new ArrayList<>();
            menuInfos.add(MenuInfo.secondAdd("药材添加","page/yaocai_add.html","iconfont layui-extend-caoyaofei","_self"));
            if(limits.getUpdatemiaosample()==1||limits.getDeletemiaosample()==1){
                menuInfos.add(MenuInfo.secondAdd("药材列表","page/yaocai_list.html","iconfont layui-extend-caoyaofei","_self"));
                menuInfos.add(MenuInfo.secondAdd("药材导入","page/FileImport.html","iconfont layui-extend-caoyaofei","_self"));
            }
            datas.add(MenuInfo.firstAdd("药材管理","","iconfont layui-extend-zhongcaoyao-01","_self",menuInfos));
        }
        menuInfos=new ArrayList<>();
        menuInfos.add(MenuInfo.secondAdd("药材查询","page/yaocai_query.html","iconfont layui-extend-caoyaofei","_self"));
        //所有人都有的权限（药材的查询）
        datas.add(MenuInfo.firstAdd("药材库","","iconfont layui-extend-zhongcaoyao","_self",menuInfos));
        map.put("homeInfo",homeInfo);
        map.put("logoInfo",logoInfo);
        map.put("menuInfo",new MenuInfo[]{MenuInfo.firstAdd("药材管理","","fa fa-user","_self",datas)});
        //System.out.println(JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }
}
